#include"rtc.h"

#include "events_init.h"
#include <stdio.h>
#include "lvgl.h"
#if defined(__has_include)
#if __has_include("ScnsLed.h")
#include "ScnsLed.h"
#endif
#endif
#if defined(__has_include)
#if __has_include("hwMotor.h")
#include "hwMotor.h"
#endif
#endif
#if defined(__has_include)
#if __has_include("hwMotor.h")
#include "hwMotor.h"
#endif
#endif

#if defined(__has_include)
#if __has_include("ScnsBeep.h")
#include "ScnsBeep.h"
#endif
#endif


void events_init(lv_ui *ui)
{
}

static void screen_imgbtn_light_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.led_del == true)
				setup_scr_led(&guider_ui);
			lv_scr_load_anim(guider_ui.led, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.screen_del = true;
		}
	}
		break;
	default:
		break;
	}
}

static void screen_imgbtn_environment_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.temperature_del == true)
				setup_scr_temperature(&guider_ui);
			lv_scr_load_anim(guider_ui.temperature, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.screen_del = true;
		}
	}
		break;
	default:
		break;
	}
}

static void screen_imgbtn_1_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.camera_del == true)
				setup_scr_camera(&guider_ui);
			lv_scr_load_anim(guider_ui.camera, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.screen_del = true;
		}
	}
		break;
	default:
		break;
	}
}

static void screen_imgbtn_2_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.motor_del == true)
				setup_scr_motor(&guider_ui);
			lv_scr_load_anim(guider_ui.motor, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.screen_del = true;
		}
	}
		break;
	default:
		break;
	}
}

static void screen_imgbtn_3_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.beep_del == true)
				setup_scr_beep(&guider_ui);
			lv_scr_load_anim(guider_ui.beep, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.screen_del = true;
		}
	}
		break;
	default:
		break;
	}
}

static void screen_imgbtn_4_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.button_del == true)
				setup_scr_button(&guider_ui);
			lv_scr_load_anim(guider_ui.button, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.screen_del = true;
		}
	}
		break;
	default:
		break;
	}
}

static void screen_imgbtn_5_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.touch_del == true)
				setup_scr_touch(&guider_ui);
			lv_scr_load_anim(guider_ui.touch, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.screen_del = true;
		}
	}
		break;
	default:
		break;
	}
}

static void screen_imgbtn_6_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.light_del == true)
				setup_scr_light(&guider_ui);
			lv_scr_load_anim(guider_ui.light, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.screen_del = true;
		}
	}
		break;
	default:
		break;
	}
}

void events_init_screen(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->screen_imgbtn_light, screen_imgbtn_light_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_imgbtn_environment, screen_imgbtn_environment_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_imgbtn_1, screen_imgbtn_1_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_imgbtn_2, screen_imgbtn_2_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_imgbtn_3, screen_imgbtn_3_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_imgbtn_4, screen_imgbtn_4_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_imgbtn_5, screen_imgbtn_5_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_imgbtn_6, screen_imgbtn_6_event_handler, LV_EVENT_ALL, ui);
}

static void temperature_btn_back_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.screen_del == true)
				setup_scr_screen(&guider_ui);
			lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.temperature_del = true;
		}
	}
		break;
	default:
		break;
	}
}

void events_init_temperature(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->temperature_btn_back, temperature_btn_back_event_handler, LV_EVENT_ALL, ui);
}

static void tample_btn_back_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.screen_del == true)
				setup_scr_screen(&guider_ui);
			lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.tample_del = true;
		}
	}
		break;
	default:
		break;
	}
}

void events_init_tample(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->tample_btn_back, tample_btn_back_event_handler, LV_EVENT_ALL, ui);
}

static void led_btn_back_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.screen_del == true)
				setup_scr_screen(&guider_ui);
			lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.led_del = true;
		}
	}
		break;
	default:
		break;
	}
}

static void led_sw_l9_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_VALUE_CHANGED:
	{
		#if defined(SCNS_LED_ENABLE)&&SCNS_LED_ENABLE==1
		  lv_obj_t*obj=lv_event_get_target(e);
		  scnsLedSet(SCNS_LED_D9,lv_obj_has_state(obj,LV_STATE_CHECKED)?SCNS_LED_SWITCH_ON:SCNS_LED_SWITCH_OFF);
		#endif
	}
		break;
	default:
		break;
	}
}

static void led_sw_l5_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_VALUE_CHANGED:
	{
		#if defined(SCNS_LED_ENABLE)&&SCNS_LED_ENABLE==1
		  lv_obj_t*obj=lv_event_get_target(e);
		  scnsLedSet(SCNS_LED_D5,lv_obj_has_state(obj,LV_STATE_CHECKED)?SCNS_LED_SWITCH_ON:SCNS_LED_SWITCH_OFF);
		#endif
	}
		break;
	default:
		break;
	}
}

static void led_sw_l4_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_VALUE_CHANGED:
	{
		#if defined(SCNS_LED_ENABLE)&&SCNS_LED_ENABLE==1
		  lv_obj_t*obj=lv_event_get_target(e);
		  scnsLedSet(SCNS_LED_D4,lv_obj_has_state(obj,LV_STATE_CHECKED)?SCNS_LED_SWITCH_ON:SCNS_LED_SWITCH_OFF);
		#endif
	}
		break;
	default:
		break;
	}
}

static void led_sw_l3_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_VALUE_CHANGED:
	{
		#if defined(SCNS_LED_ENABLE)&&SCNS_LED_ENABLE==1
		  lv_obj_t*obj=lv_event_get_target(e);
		  scnsLedSet(SCNS_LED_D3,lv_obj_has_state(obj,LV_STATE_CHECKED)?SCNS_LED_SWITCH_ON:SCNS_LED_SWITCH_OFF);
		#endif
	}
		break;
	default:
		break;
	}
}

static void led_sw_l7_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_VALUE_CHANGED:
	{
		#if defined(SCNS_LED_ENABLE)&&SCNS_LED_ENABLE==1
		  lv_obj_t*obj=lv_event_get_target(e);
		  scnsLedSet(SCNS_LED_D7,lv_obj_has_state(obj,LV_STATE_CHECKED)?SCNS_LED_SWITCH_ON:SCNS_LED_SWITCH_OFF);
		#endif
	}
		break;
	default:
		break;
	}
}

static void led_sw_l8_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_VALUE_CHANGED:
	{
		#if defined(SCNS_LED_ENABLE)&&SCNS_LED_ENABLE==1
		  lv_obj_t*obj=lv_event_get_target(e);
		  scnsLedSet(SCNS_LED_D8,lv_obj_has_state(obj,LV_STATE_CHECKED)?SCNS_LED_SWITCH_ON:SCNS_LED_SWITCH_OFF);
		#endif
	}
		break;
	default:
		break;
	}
}

void events_init_led(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->led_btn_back, led_btn_back_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->led_sw_l9, led_sw_l9_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->led_sw_l5, led_sw_l5_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->led_sw_l4, led_sw_l4_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->led_sw_l3, led_sw_l3_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->led_sw_l7, led_sw_l7_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->led_sw_l8, led_sw_l8_event_handler, LV_EVENT_ALL, ui);
}

static void camera_btn_back_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.screen_del == true)
				setup_scr_screen(&guider_ui);
			lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.camera_del = true;
		}
	}
		break;
	default:
		break;
	}
}

void events_init_camera(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->camera_btn_back, camera_btn_back_event_handler, LV_EVENT_ALL, ui);
}

static void motor_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		#ifdef __HW_MOTOR_H__
		lv_obj_t*obj=lv_event_get_target(e);
		hwMotorSetPwm(lv_slider_get_value(obj));
		#endif
		
	}
		break;
	default:
		break;
	}
}

static void motor_btn_back_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.screen_del == true)
				setup_scr_screen(&guider_ui);
			lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.motor_del = true;
		}
	}
		break;
	default:
		break;
	}
}

static void motor_slider_1_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_VALUE_CHANGED:
	{
		#ifdef __HW_MOTOR_H__
		lv_obj_t*obj=lv_event_get_target(e);
		hwMotorSetPwm(lv_slider_get_value(obj));
		#endif
	}
		break;
	default:
		break;
	}
}

void events_init_motor(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->motor, motor_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->motor_btn_back, motor_btn_back_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->motor_slider_1, motor_slider_1_event_handler, LV_EVENT_ALL, ui);
}

static void beep_btn_back_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.screen_del == true)
				setup_scr_screen(&guider_ui);
			lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.beep_del = true;
		}
	}
		break;
	default:
		break;
	}
}

static void beep_btnm_1_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		#if defined(SCNS_BEEP_ENABLE)&&SCNS_BEEP_ENABLE==1
		lv_obj_t*obj=lv_event_get_target(e);
		const char * txt=lv_btnmatrix_get_btn_text(obj, lv_btnmatrix_get_selected_btn(obj));
		scnsBeepAdd(txt);
		#endif
		
	}
		break;
	default:
		break;
	}
}

void events_init_beep(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->beep_btn_back, beep_btn_back_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->beep_btnm_1, beep_btnm_1_event_handler, LV_EVENT_ALL, ui);
}

static void button_btn_back_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.screen_del == true)
				setup_scr_screen(&guider_ui);
			lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.button_del = true;
		}
	}
		break;
	default:
		break;
	}
}

void events_init_button(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->button_btn_back, button_btn_back_event_handler, LV_EVENT_ALL, ui);
}

static void touch_btn_back_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.screen_del == true)
				setup_scr_screen(&guider_ui);
			lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.touch_del = true;
		}
	}
		break;
	default:
		break;
	}
}

void events_init_touch(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->touch_btn_back, touch_btn_back_event_handler, LV_EVENT_ALL, ui);
}

static void light_btn_back_event_handler(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
		lv_obj_t * act_scr = lv_scr_act();
		lv_disp_t * d = lv_obj_get_disp(act_scr);
		if (d->prev_scr == NULL && (d->scr_to_load == NULL || d->scr_to_load == act_scr))
		{
			lv_obj_clean(act_scr);
			if (guider_ui.screen_del == true)
				setup_scr_screen(&guider_ui);
			lv_scr_load_anim(guider_ui.screen, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
			lv_obj_del(act_scr);
			guider_ui.light_del = true;
		}
	}
		break;
	default:
		break;
	}
}

void events_init_light(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->light_btn_back, light_btn_back_event_handler, LV_EVENT_ALL, ui);
}
